﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using System.Collections;

namespace LCWMS
{
    public partial class w_inv002 : DockContent
    {
        private DataSet dscustomer;
        //返回的商品信息 格式sku
        public static string str_skucode;
        private int i_trans_header;
        //记录升降序
        //private bool b_sort; //true升序，否则降序
        private bool b_trans_sort;

        public static string s_inv002_con;
        public static string s_inv002_billno;
        DataSet ds_tran;
        
        public w_inv002()
        {
            InitializeComponent();
            dgv_tran.AutoGenerateColumns = false;
        }
        //添加库存交易的合计行
        private void f_add_transf()
        {
            decimal d_1, d_2, d_3, d_4, d_5,d_6,d_7,d_8,d_9,d_10;
            DataTable dt_put;
            //增加一个合计行 
            dt_put = (DataTable)dgv_tran.DataSource;
            d_1 = (decimal)dt_put.Compute("Sum(" + dt_put.Columns["stock_qty_m"].ColumnName + ")", "true");
            d_2 = (decimal)dt_put.Compute("Sum(" + dt_put.Columns["stock_qty_s"].ColumnName + ")", "true");
            d_3 = (decimal)dt_put.Compute("Sum(" + dt_put.Columns["cubage"].ColumnName + ")", "true");
            d_4 = (decimal)dt_put.Compute("Sum(" + dt_put.Columns["gross_weight"].ColumnName + ")", "true");
            d_5 = (decimal)dt_put.Compute("Sum(" + dt_put.Columns["net_weight"].ColumnName + ")", "true");
            d_6 = (decimal)dt_put.Compute("Sum(" + dt_put.Columns["new_cubage"].ColumnName + ")", "true");
            d_7 = (decimal)dt_put.Compute("Sum(" + dt_put.Columns["new_gross_weight"].ColumnName + ")", "true");
            d_8 = (decimal)dt_put.Compute("Sum(" + dt_put.Columns["new_net_weight"].ColumnName + ")", "true");
            d_9 = (decimal)dt_put.Compute("Sum(" + dt_put.Columns["happen_qty_m"].ColumnName + ")", "true");
            d_10 = (decimal)dt_put.Compute("Sum(" + dt_put.Columns["happen_qty_s"].ColumnName + ")", "true");
            //d_11 = (decimal)dt_put.Compute("Sum(" + dt_put.Columns["box"].ColumnName + ")", "true");
            DataRow dr = dt_put.NewRow();
            dr["pack_code"] = "合计";
            dr["stock_qty_m"] = d_1;
            dr["stock_qty_s"] = d_2;
            dr["cubage"] = d_3;
            dr["gross_weight"] = d_4;
            dr["net_weight"] = d_5;

            dr["new_cubage"] = d_6;
            dr["new_gross_weight"] = d_7;
            dr["new_net_weight"] = d_8;
            dr["happen_qty_m"] = d_9;
            dr["happen_qty_s"] = d_10;
            //dr["box"] = d_11;

            ((DataTable)dgv_tran.DataSource).Rows.InsertAt(dr, ((DataTable)dgv_tran.DataSource).Rows.Count);
        }

        private void btn_Query_Click(object sender, EventArgs e)
        {
            string str_sql_where = " where a.inv_org = '"+main.g_org+"'  ";
            string str_sql = "";
            DataSet ds_transaction = null;  
            
            if (dtp_h_time.Text != "")
            {
                if(main.dbtype =="SQL")
                    str_sql_where += " and CONVERT(varchar(10) , a.happen_time, 102 )  between     '" + dtp_h_time.Text.Trim() + "' and '" + dtp_h_time_end.Text + "'";
                else
                    str_sql_where += " and to_char(a.happen_time,'yyyy.mm.dd')  between     '" + dtp_h_time.Text.Trim() + "' and '" + dtp_h_time_end.Text + "'";
            }
            if (tb_bill_no.Text != "")
            {
                str_sql_where += " and a.bill_no='" + tb_bill_no.Text.Trim() + "'";
            }
            //if (cb_type.SelectedValue.ToString() != "")
            //{
            //    str_sql_where += " and a.transaction_type='" + cb_type.SelectedValue.ToString().Trim() + "'";
            //}
            if (PowerComboBox3.Text.Trim() != "")
            {
                string str_tyep = "", str_where = "", str_code = "";
                str_tyep = PowerComboBox3.Text.Trim();
                string[] arr = str_tyep.Split(',');
                for (int i = 0; i < arr.Length; i++)
                {
                    str_where = "transaction_typ = '" + arr[i].Trim() + "'";
                    DataRow[] tr = ds_tran.Tables[0].Select(str_where);
                    if (tr.Length > 0)
                    {
                        if (str_code.Trim().Length == 0)
                        {
                            str_code = "'" + tr[0]["dictionary_code"].ToString() + "'";
                        }
                        else
                        {
                            str_code += ",'" + tr[0]["dictionary_code"].ToString().Trim() + "'";
                        }
                    }
                }
                if (str_code.Trim() != "")
                {
                    str_sql_where += " and a.transaction_type in (" + str_code + ")";
                }
            }
            if (tb_batchno.Text != "")
            {
                str_sql_where += " and a.batchno='" + tb_batchno.Text.Trim() + "'";
            }
            if (tb_location_code.Text != "")
            {
                str_sql_where += " and a.location_code='" + tb_location_code.Text.Trim() + "'";
            }
            if (tb_pallet_no.Text != "")
            {
                str_sql_where += " and a.pallet_no='" + tb_pallet_no.Text.Trim() + "'";
            }
            if (cb_consignor.Text != "")
            {
                str_sql_where += " and a.consignor='" + cb_consignor.Text.Trim() + "'";
            }
            else
            {
                str_sql_where += " and  a.consignor in (select mst_1.consignor from " + main.g_space + ".mst_customer mst_1," + main.g_space + ".sys_user mst_2," + main.g_space + ".sys_user_customer mst_3" +
                             " where mst_2.inv_org = mst_3.inv_org and mst_2.user_id = mst_3.user_id and mst_3.inv_org = mst_1.inv_org and mst_3.consignor = mst_1.consignor and mst_2.inv_org = '"+main.g_org+"' and mst_2.user_id = '" + main.g_user + "' and mst_1.consignor_flag='1' )";
            }
            if (tb_sku.Text != "")
            {
                str_sql_where += " and a.sku = '" + tb_sku.Text.Trim() + "'";
            }
            if (tb_sku_name.Text != "")  
            { 
                str_sql_where += " and b.sku_name like '%" + tb_sku_name.Text.Trim() + "%'";
            }
            str_sql = "select a.happen_time,a.transaction_type,RTRIM(a.bill_no) bill_no,a.bill_line_no,a.batchno,RTRIM(a.location_code) location_code,a.pallet_no," +
                             "row_number() over(ORDER BY a.transaction_id) as xh,a.consignor,RTRIM(a.sku) sku,b.sku_name,a.order_price,a.order_unit,a.new_batchno,a.new_location_code,a.new_pallet_no,a.new_consignor," +
                              "a.new_sku,a.new_order_price,a.new_order_unit,a.pack_code,a.main_sub_flag,a.convert_modulus,a.shelflife_day,a.stock_qty_m," +
                              "a.stock_qty_s,a.cubage,a.gross_weight,a.net_weight,a.new_cubage,a.new_gross_weight,a.new_net_weight,a.happen_qty_m," +
                              "a.happen_qty_s,a.renson_code,a.lot_attribute1,a.lot_attribute2,a.lot_attribute3,a.lot_attribute4,a.lot_attribute5,RTRIM(a.lot_attribute6) lot_attribute6 ,a.lot_attribute7,a.lot_attribute8," +
                              "a.lot_attribute9,a.lot_attribute10,a.lot_attribute11,a.lot_attribute12,a.lot_attribute13,a.lot_attribute14,a.lot_attribute15,a.standby_3," +
                              "case when upper(a.transaction_type) = 'AD' then case a.bill_type when '1' then '" + main.g_class.comm_getchangestring("Check Adjust", main.g_info_dataset) + "' when '2' then '" + main.g_class.comm_getchangestring("scrap", main.g_info_dataset) + "' else '" + main.g_class.comm_getchangestring("normal", main.g_info_dataset) + "' end " +
                              " when upper(a.transaction_type) = 'FR'then case a.bill_type when '0' then '" + main.g_class.comm_getchangestring("sku", main.g_win_dataset) + "' when '1' then '" + main.g_class.comm_getchangestring("location & Sku", main.g_win_dataset) + "' when '2' then '" + main.g_class.comm_getchangestring("pallet", main.g_win_dataset) + "' when '3' then '" + main.g_class.comm_getchangestring("Location", main.g_win_dataset) + "' else '" + main.g_class.comm_getchangestring("Batch", main.g_win_dataset) + "' end " +
                              " when upper(a.transaction_type) = 'MV'then case a.bill_type when '01' then '" + main.g_class.comm_getchangestring("import store", main.g_info_dataset) + "' when '02' then '" + main.g_class.comm_getchangestring("borrow store", main.g_info_dataset) + "' when '03' then '" + main.g_class.comm_getchangestring("kanban store", main.g_info_dataset) + "' else '" + main.g_class.comm_getchangestring("move", main.g_info_dataset) + "' end " +
                              " when upper(a.transaction_type) in ('PA','QC','PC') then case a.bill_type when '0' then '" + main.g_class.comm_getchangestring("Normal", main.g_info_dataset) + "' when '1' then '" + main.g_class.comm_getchangestring("CROSS DOCKING", main.g_info_dataset) + "' when '2' then '" + main.g_class.comm_getchangestring("CONSIGNMENT MOVE", main.g_info_dataset) + "' when '3' then '" + main.g_class.comm_getchangestring("Consignment", main.g_info_dataset) + "' when '4' then '" + main.g_class.comm_getchangestring("Back", main.g_info_dataset) + "' when '5' then '" + main.g_class.comm_getchangestring("Appropriation", main.g_info_dataset) + "' when '6' then  '" + main.g_class.comm_getchangestring("Other", main.g_info_dataset) + "' when '7' then '" + main.g_class.comm_getchangestring("Sale Return", main.g_info_dataset) + "' when '8' then '" + main.g_class.comm_getchangestring("delegate", main.g_info_dataset) + "' when '9' then '" + main.g_class.comm_getchangestring("outsourcing", main.g_info_dataset) + "' else '' end " +
                              " when upper(a.transaction_type) in ('PK','SS','SC') then case a.bill_type when '0' then '" + main.g_class.comm_getchangestring("Outbound", main.g_info_dataset) + "' when '1' then '" + main.g_class.comm_getchangestring("Return", main.g_info_dataset) + "' when '2' then '" + main.g_class.comm_getchangestring("Consignment ", main.g_info_dataset) + "' when '3' then '" + main.g_class.comm_getchangestring("Appropriation ", main.g_info_dataset) + "' when '4' then '" + main.g_class.comm_getchangestring("Sale", main.g_info_dataset) + "'  when '5' then '" + main.g_class.comm_getchangestring("Other ", main.g_info_dataset) + "' when '6' then '" + main.g_class.comm_getchangestring("delegate", main.g_info_dataset) + "'  when '7' then '" + main.g_class.comm_getchangestring("outsourcing", main.g_info_dataset) + "' when '8' then '" + main.g_class.comm_getchangestring("guarantees", main.g_info_dataset) + "' when '9' then '" + main.g_class.comm_getchangestring("Consignment Move", main.g_info_dataset) + "' else '' end " +
                              " when upper(a.transaction_type) in ('IN','DP') then case a.bill_type when '1' then '" + main.g_class.comm_getchangestring("Outbound", main.g_info_dataset) + "' when '2' then '" + main.g_class.comm_getchangestring("Outbound", main.g_info_dataset) + "' when '3' then '" + main.g_class.comm_getchangestring("Outbound", main.g_info_dataset) + "' when '9' then '" + main.g_class.comm_getchangestring("Outbound", main.g_info_dataset) + "' else '" + main.g_class.comm_getchangestring("Outbound", main.g_info_dataset) + "' end else '' end bill_type," +
                             " a.memo,a.main_dist,a.made_time,a.maker  from " + main.g_space + ".inv_transaction a left join " + main.g_space + ".mst_sku b  on a.inv_org = b.inv_org and a.consignor=b.consignor and a.sku=b.sku " + str_sql_where;
            try  
            {  
                if (main.dbtype == "SQL")
                {
                    ds_transaction = main.dbservice.Sql_exec_selectsql(str_sql);

                }
                else
                {
                    ds_transaction = main.dbservice.oracle_exec_selectsql(str_sql);
                }
                if(ds_transaction.Tables[0].Rows.Count > 0)
                  dgv_tran.DataSource = ds_transaction.Tables[0];
                  b_trans_sort= true;
                  if (ds_transaction.Tables[0].Rows.Count > 0)
                  {
                      f_add_transf();
                  }
                  else
                      dgv_tran.DataSource = null;
            }
            catch (System.Exception ee)
            {
                // MessageBox.Show("提取查询信息失败!" + ee.ToString());
                MessageBox.Show(main.g_class.comm_getchangestring("Fail to Query", main.g_info_dataset) + "!" + ee.ToString());
            }
        }

        private void w_inv002_Load(object sender, EventArgs e)
        {
            //获取按钮权限
            string str_right;
            str_right = main.g_class.comm_getright("w_inv002", main.g_right_dataset);
            //导出
            if (str_right.Substring(0, 1) == "0")
            {
                btn_excel.Visible = false;
            }
            else
            {
                btn_excel.Visible = true;
            }
            //初始化变量
            dscustomer = new DataSet();
            
            string str_common = "";
            DataSet ds_type = null;
            try
            {
                dscustomer = new DataSet();
                dscustomer = main.g_class.comm_get_consignor();
                cb_consignor.DataSource = dscustomer.Tables[0];
                cb_consignor.ValueMember = "consignor";
                cb_consignor.DisplayMember = "consignor";

                if (main.dbtype == "SQL")
                {
                    str_common = "select dictionary_code,case use_flag when '1' then content1 when '2' then content2 when '3' then convert(char,number1) else convert(char,number2) end as transaction_typ from " + main.g_space + ".sys_dictionary where inv_org = '"+main.g_org+"' and ltrim(rtrim(dictionary_name))='transaction_typ' order by dictionary_code";
                    ds_type = main.dbservice.Sql_exec_selectsql(str_common);
                }
                else
                {
                    str_common = "select dictionary_code,case use_flag when '1' then content1 when '2' then content2 when '3' then to_char(number1) else to_char(number2) end as transaction_typ from " + main.g_space + ".sys_dictionary where inv_org = '" + main.g_org + "' and trim(dictionary_name)='transaction_typ' order by dictionary_code";
                    ds_type = main.dbservice.oracle_exec_selectsql(str_common);
                }
                //进行语言转换
                int i_ds;
                i_ds = ds_type.Tables[0].Rows.Count;
                if (i_ds > 0)
                {
                    for (int i = 0; i < i_ds; i++)
                    {
                        ds_type.Tables[0].Rows[i]["transaction_typ"] = main.g_class.comm_getchangestring(ds_type.Tables[0].Rows[i]["transaction_typ"].ToString().Trim(), main.g_info_dataset);
                        ds_type.Tables[0].Rows[i]["dictionary_code"] = main.g_class.comm_getchangestring(ds_type.Tables[0].Rows[i]["dictionary_code"].ToString().Trim(), main.g_info_dataset);
                    }
                }
                DataSet newf_ds = ds_type.Copy();
                ds_tran = ds_type.Copy();
                DataGridViewComboBoxColumn dgvComboBoxColumn = dgv_tran.Columns["transaction_type"] as DataGridViewComboBoxColumn;
                dgvComboBoxColumn.DataPropertyName = "transaction_type";
                dgvComboBoxColumn.DataSource = newf_ds.Tables[0];//必须在设置dataGridView1的DataSource的属性前设置
                dgvComboBoxColumn.DisplayMember = "transaction_typ";
                dgvComboBoxColumn.ValueMember = "dictionary_code";

                DataRow dr = ds_type.Tables[0].NewRow();
                dr["dictionary_code"] = "";
                dr["transaction_typ"] = "";
                ds_type.Tables[0].Rows.Add(dr);
                 DataView DV = ds_type.Tables[0].Copy().DefaultView;
                DV.Sort = "dictionary_code asc";
                PowerComboBox3.DataSource = DV;
                PowerComboBox3.ValueMember = "dictionary_code";
                PowerComboBox3.DisplayMember = "transaction_typ";
               
                main.g_class.comm_changeControl(this, main.g_win_dataset);
            }
            catch (System.Exception ee)
            {
                MessageBox.Show(main.g_class.comm_getchangestring("Fail to initialize Information of transaction type", main.g_info_dataset) + "!" + ee.ToString());
                return;
            }
            ////转换右键菜单
            //if (CMS_DISPLAY.Items.Count > 0)
            //{
            //    for (int i = 0; i < CMS_DISPLAY.Items.Count; i++)
            //    {
            //        CMS_DISPLAY.Items[i].Text = main.g_class.comm_getchangestring(CMS_DISPLAY.Items[i].Text.Trim(), main.g_info_dataset);
            //        //if (main.g_class.comm_find_module(CMS_DISPLAY.Items[i].Tag.ToString().Trim()))
            //        //{
            //        //    CMS_DISPLAY.Items[i].Enabled = true;
            //        //}
            //        //else
            //        //{
            //        //    CMS_DISPLAY.Items[i].Enabled = false;
            //        //}
            //    }
            //}
            dtp_h_time.CustomFormat = "yyyy.MM.dd";
            dtp_h_time.Format = DateTimePickerFormat.Custom;
            dtp_h_time_end.CustomFormat = "yyyy.MM.dd";
            dtp_h_time_end.Format = DateTimePickerFormat.Custom;
            dtp_h_time.Value = System.Convert.ToDateTime(DateTime.Now.ToString("yyyy.MM.dd").Trim());
            dtp_h_time_end.Value = System.Convert.ToDateTime(DateTime.Now.AddDays(7).ToString("yyyy.MM.dd").Trim());

            s_inv002_con = "";
            s_inv002_billno = "";
        }

        private void btn_sku_Click(object sender, EventArgs e)
        {
            string str_customer;
            w_single wsingle = new w_single();
            str_customer = "";
            w_single.str_qcustomer = str_customer;
            wsingle.ShowDialog();
            this.tb_sku_name.Text = w_single.str_qsku;
        }

        private void dgv_tran_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void lb_batchno_Click(object sender, EventArgs e)
        {

        }

        private void dgv_tran_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            i_trans_header = e.ColumnIndex;

        }

        private void dgv_tran_Sorted(object sender, EventArgs e)
        {
            string str_headertext;
            //先删除合计行
            ((DataTable)dgv_tran.DataSource).Rows.Remove(((DataTable)dgv_tran.DataSource).Select("pack_code = '合计'")[0]);
            //然后对Tables[0]重新排序
            DataTable dt;
            DataView dv = ((DataTable)dgv_tran.DataSource).DefaultView;
            //得到点击的列
            str_headertext = dgv_tran.Columns[i_trans_header].DataPropertyName.ToString().Trim();
            if (b_trans_sort) //升序
            {
                dv.Sort = str_headertext + " DESC";
                b_trans_sort = false;
            }
            else
            {
                dv.Sort = str_headertext;
                b_trans_sort = true;
            }
            dt = dv.ToTable();
            dgv_tran.DataSource = null;
            dgv_tran.DataSource = dt;
            //增加合计行
            f_add_transf();
        }

        private void cb_consignor_TextChanged(object sender, EventArgs e)
        {
            string str_cust;
            str_cust = cb_consignor.Text.Trim();
            tb_cusname.Text = main.g_class.comm_findname(str_cust, dscustomer);
        }

        private void dgv_tran_DoubleClick(object sender, EventArgs e)
        {
            int i_currow;
            string str_tran_type;
            //双击根据业务类型的不同跳转到相应的业务查询界面
           // TR   TK    RP  PA    PK  MV    IN   DP  AD     MN   FR   
            if (dgv_tran.CurrentRow != null)
            {
                i_currow = dgv_tran.CurrentRow.Index;
                if (i_currow >= 0) //说明有选中的行
                {
                    //得到货主和相应的单据号
                    s_inv002_con = dgv_tran.Rows[i_currow].Cells["consignor"].EditedFormattedValue.ToString().Trim();
                    s_inv002_billno = dgv_tran.Rows[i_currow].Cells["bill_no"].EditedFormattedValue.ToString().Trim();
                    str_tran_type = dgv_tran.Rows[i_currow].Cells["transaction_type"].Value.ToString().Trim();
                    if (str_tran_type.ToUpper().Trim() == "TR") //转移
                    {
                        w_inv009 inv009 = new w_inv009();
                        inv009.Text = main.g_class.comm_getchangestring("Transfer Query", main.g_info_dataset);
                        inv009.Show(main.dp_pop, DockState.Document);
                    }
                    else if (str_tran_type.ToUpper().Trim() == "PA") //入库
                    {
                        w_asn008 asn008 = new w_asn008();
                        asn008.Text = main.g_class.comm_getchangestring("ASN Query", main.g_info_dataset);
                        asn008.Show(main.dp_pop, DockState.Document);
                    }
                    else if (str_tran_type.ToUpper().Trim() == "TK") //临时领料
                    {
                        w_so022 so022 = new w_so022();
                        so022.Text = main.g_class.comm_getchangestring("Temp OutBound Query", main.g_info_dataset);
                        so022.Show(main.dp_pop, DockState.Document);
                    }
                    else if (str_tran_type.ToUpper().Trim() == "RP" || str_tran_type.ToUpper().Trim() == "MV" || str_tran_type.ToUpper().Trim() == "MN") //补货 \移库\无任务移库
                    {
                        w_inv008 inv008 = new w_inv008();
                        inv008.Text = main.g_class.comm_getchangestring("Move Query", main.g_info_dataset);
                        inv008.Show(main.dp_pop, DockState.Document);
                    }
                    else if (str_tran_type.ToUpper().Trim() == "PK") //拣货
                    {
                        w_so008 so008 = new w_so008();
                        so008.Text = main.g_class.comm_getchangestring("SO Query", main.g_info_dataset);
                        so008.Show(main.dp_pop, DockState.Document);
                    }
                    else if (str_tran_type.ToUpper().Trim() == "FR") //冻结
                    {
                        w_inv011 inv011 = new w_inv011();
                        inv011.Text = main.g_class.comm_getchangestring("Freeze Query", main.g_info_dataset);
                        inv011.Show(main.dp_pop, DockState.Document);
                    }
                    else if (str_tran_type.ToUpper().Trim() == "AD") //调整
                    {
                        w_inv010 inv010 = new w_inv010();
                        inv010.Text = main.g_class.comm_getchangestring("Adjust Query", main.g_info_dataset);
                        inv010.Show(main.dp_pop, DockState.Document);
                    }
                    else if (str_tran_type.ToUpper().Trim() == "DP") //委外出库
                    {
                        w_so028 so028 = new w_so028();
                        so028.Text = main.g_class.comm_getchangestring("Delegate OutBound Query", main.g_info_dataset);
                        so028.Show(main.dp_pop, DockState.Document);
                    }
                    else if (str_tran_type.ToUpper().Trim() == "IN") //委外入库
                    {
                        w_asn027 asn027 = new w_asn027();
                        asn027.Text = main.g_class.comm_getchangestring("Delegate Receive Query", main.g_info_dataset);
                        asn027.Show(main.dp_pop, DockState.Document);
                    }
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string str_tyep = "", str_sql_where="",str_code="";            
            str_tyep = PowerComboBox3.Text.Trim();
            string[] arr = str_tyep.Split(',');
            for (int i = 0; i < arr.Length; i++)
            {               
                str_sql_where = "transaction_typ = '" + arr[i].Trim() + "'";
                DataRow[] tr = ds_tran.Tables[0].Select(str_sql_where);
                if (tr.Length > 0)
                {
                    if (str_code.Trim().Length == 0)
                    {
                        str_code = "'" + tr[0]["dictionary_code"].ToString() + "'";                      
                    }
                    else
                    {
                        str_code += ",'" + tr[0]["dictionary_code"].ToString().Trim() + "'";
                    }                   
                }
            }
          
        }

        private void PowerComboBox3_Click(object sender, EventArgs e)
        {
           
        }

        private void aSNQueryToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ////首先要找到
            //IDockContent curidc;
            //curidc = main.g_class.Findwindows(aSNQueryToolStripMenuItem.Text.Trim());
            //if (curidc != null) //说明已经打开
            //{
            //    curidc.DockHandler.Close();
            //}
            ////然后重新打开
            //if (dgv_tran.CurrentRow != null)
            //{
            //    if (dgv_tran.CurrentRow.Index >= 0)
            //    {
            //        string str_no = dgv_tran.Rows[dgv_tran.CurrentRow.Index].Cells["bill_no"].EditedFormattedValue.ToString().Trim();
            //        if (str_no.Substring(0, 2) == "AN")
            //        {
            //            w_asn008.s_pop_asnno = str_no;
            //            w_asn008.s_pop_consignor = dgv_tran.Rows[dgv_tran.CurrentRow.Index].Cells["consignor"].EditedFormattedValue.ToString().Trim();
            //        }
            //        else
            //        {
            //            MessageBox.Show("选择的单据不是入库单据!");
            //            return;
            //        }
            //    }
            //}
            //w_asn008 asn008 = new w_asn008();
            //asn008.Text = aSNQueryToolStripMenuItem.Text.Trim();
            //asn008.Show(main.dp_pop, DockState.Document);
        }

        private void sOQueryToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ////首先要找到
            //IDockContent curidc;
            //curidc = main.g_class.Findwindows(sOQueryToolStripMenuItem.Text.Trim());
            //if (curidc != null) //说明已经打开
            //{
            //    curidc.DockHandler.Close();
            //}
            ////然后重新打开
            //if (dgv_tran.CurrentRow != null)
            //{
            //    if (dgv_tran.CurrentRow.Index >= 0)
            //    {
            //        string str_no = dgv_tran.Rows[dgv_tran.CurrentRow.Index].Cells["bill_no"].EditedFormattedValue.ToString().Trim();
            //        if (str_no.Substring(0, 2) == "SO")
            //        {
            //            w_so008.s_pop_sono = str_no;
            //            w_so008.s_pop_consignor = dgv_tran.Rows[dgv_tran.CurrentRow.Index].Cells["consignor"].EditedFormattedValue.ToString().Trim();
            //        }
            //        else
            //        {
            //            MessageBox.Show("选择的单据不是出库单据!");
            //            return;
            //        }
            //    }
            //}
            //w_so008 so008 = new w_so008();
            //so008.Text = sOQueryToolStripMenuItem.Text.Trim();
            //so008.Show(main.dp_pop, DockState.Document);
        }

        private void moveQueryToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ////首先要找到
            //IDockContent curidc;
            //curidc = main.g_class.Findwindows(moveQueryToolStripMenuItem.Text.Trim());
            //if (curidc != null) //说明已经打开
            //{
            //    curidc.DockHandler.Close();
            //}
            ////然后重新打开
            //if (dgv_tran.CurrentRow != null)
            //{
            //    if (dgv_tran.CurrentRow.Index >= 0)
            //    {
            //        string str_no = dgv_tran.Rows[dgv_tran.CurrentRow.Index].Cells["bill_no"].EditedFormattedValue.ToString().Trim();
            //        if (str_no.Substring(0, 2) == "MV")
            //        {
            //            w_inv008.s_pop_moveno = str_no;
            //            w_inv008.s_pop_consignor = dgv_tran.Rows[dgv_tran.CurrentRow.Index].Cells["consignor"].EditedFormattedValue.ToString().Trim();
            //        }
            //        else
            //        {
            //            MessageBox.Show("选择的单据不是移库单据!");
            //            return;
            //        }
            //    }
            //}
            //w_inv008 inv008 = new w_inv008();
            //inv008.Text = sOQueryToolStripMenuItem.Text.Trim();
            //inv008.Show(main.dp_pop, DockState.Document);
        }

        private void transferQueryToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ////首先要找到
            //IDockContent curidc;
            //curidc = main.g_class.Findwindows(moveQueryToolStripMenuItem.Text.Trim());
            //if (curidc != null) //说明已经打开
            //{
            //    curidc.DockHandler.Close();
            //}
            ////然后重新打开
            //if (dgv_tran.CurrentRow != null)
            //{
            //    if (dgv_tran.CurrentRow.Index >= 0)
            //    {
            //        string str_no = dgv_tran.Rows[dgv_tran.CurrentRow.Index].Cells["bill_no"].EditedFormattedValue.ToString().Trim();
            //        if (str_no.Substring(0, 2) == "TR")
            //        {
            //            w_inv009.s_pop_trno = str_no;
            //            w_inv009.s_pop_consignor = dgv_tran.Rows[dgv_tran.CurrentRow.Index].Cells["consignor"].EditedFormattedValue.ToString().Trim();
            //        }
            //        else
            //        {
            //            MessageBox.Show("选择的单据不是转移单据!");
            //            return;
            //        }
            //    }
            //}
            //w_inv009 inv009 = new w_inv009();
            //inv009.Text = sOQueryToolStripMenuItem.Text.Trim();
            //inv009.Show(main.dp_pop, DockState.Document);
        }

        private void btn_excel_Click(object sender, EventArgs e)
        {
            main.g_class.DataGridViewToExcel(dgv_tran);
        }

        // 鼠标右键删除行
        private void dgv_tran_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                ContextMenu m = new ContextMenu(); // 创建一个 ContextMenu
                MenuItem deleteRowItem = new MenuItem("删除行"); // 创建一个 MenuItem
                deleteRowItem.Click += dgv_tran_DeleteRowItem_Click; // 添加点击事件
                m.MenuItems.Add(deleteRowItem);

                int currentMouseOverRow = dgv_tran.HitTest(e.X, e.Y).RowIndex;   // 将 MenuItem 添加到 ContextMenu
                m.Show(dgv_tran, new Point(e.X, e.Y));                           // 显示 ContextMenu
            }
        }

        private void dgv_tran_DeleteRowItem_Click(object sender, EventArgs e)
        {

            w_dgv_deleterow wsingle = new w_dgv_deleterow();
            wsingle.ShowDialog();
            int i_row_start = w_dgv_deleterow.i_row_start - 1;
            int i_row_end = w_dgv_deleterow.i_row_end - 1;

            // 确保起始行和结束行在有效范围内
            if (i_row_start < 0) return;
            if (i_row_end >= dgv_tran.Rows.Count || i_row_start > i_row_end)
            {
                MessageBox.Show("超出有效范围！");
                return;
            }

            // 从 DataGridView 中删除指定范围的行 (从后向前删除行，避免索引变化问题)
            for (int i = i_row_end; i >= i_row_start; i--)
            {
                dgv_tran.Rows.RemoveAt(i);
            }

        }

    }
}